<?php
class Exts_Controller_SecureRemoteAction extends Exts_Controller_SecureAction
{

	protected $_certificate = NULL;
	protected $_endPoint = "/controller/rest";
	
	protected function _checkSecurity () {
		
		if ($this->_certificate == NULL){
			$country = $this->_config->settings->{'country'};
			$domain =   $this->_config->countries->{$country}->url;
			$username = $this->_config->countries->{$country}->user;
			$password = $this->_config->countries->{$country}->password;
			
			$client = new Zend_Rest_Client(
				'http://' .$domain. $this->_endPoint
			); 

			$secureuser = new Exts_Security_SecureUser($username, $password);
			try{
				$certificate = $client->validateUser($secureuser->getSecureData())->get();	
			}
			catch(Exception $e) {
				echo $e->getMessage();exit;
			}
			
			if ($client->isSuccess()){
				$this->_certificate = $certificate->validateUser->response[0];
				if ($this->_certificate != "invalid certificate"){
					$this->_certificate = Exts_Security_Certificate::Load($this->_certificate);
					$this->_user = new stdClass();
					$this->_user->id = $this->_certificate->userId;
					return true;
				}
			} 
			else {
				throw new Exts_Exception ("Error al verificar usuario!");
			}
			return false;
			
		}
		return true;
	}
	public function preDispatch() {
		$this->_checkSecurity();	
/*		if (!$this->_checkSecurity()){
			throw new Exts_Exception ("Error al registrar usuario contra el servicio remoto");
		}*/
		return parent::preDispatch();
	}		
}
